package cn.lzwei.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义校验注解@ListValue 的Integer校验器
 */
public class StatusConstraintValidatorForInteger implements ConstraintValidator<ListValue,Integer> {

    Set<Integer> set=new HashSet<>();
    //初始化：获取可接受的数据集合
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] value = constraintAnnotation.value();
        if (value!=null || value.length>0){
            for (int i : value) {
                set.add(i);
            }
        }
    }
    //判断：值是否可接受
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
